#!/bin/bash

set -e

log-debug "bootstrapping downstream agent..."
docker compose exec -T downstream-spire-server \
    /opt/spire/bin/spire-server bundle show > conf/downstream/agent/bootstrap.crt

log-debug "bootstrapping upstream agent..."
docker compose exec -T upstream-spire-server \
    /opt/spire/bin/spire-server bundle show > conf/upstream/agent/bootstrap.crt

log-debug "bootstrapping bundle from downstream to upstream server..."
docker compose exec -T downstream-spire-server \
    /opt/spire/bin/spire-server bundle show -format spiffe > conf/upstream/server/downstream-domain.test.bundle

# On macOS, there can be a delay propagating the file on the bind mount to the other container
sleep 1

docker compose exec -T upstream-spire-server \
    /opt/spire/bin/spire-server bundle set -format spiffe -id spiffe://downstream-domain.test -path /opt/spire/conf/server/downstream-domain.test.bundle

log-debug "bootstrapping bundle from upstream to downstream server..."
docker compose exec -T upstream-spire-server \
    /opt/spire/bin/spire-server bundle show -format spiffe > conf/downstream/server/upstream-domain.test.bundle

# On macOS, there can be a delay propagating the file on the bind mount to the other container
sleep 1

docker compose exec -T downstream-spire-server \
    /opt/spire/bin/spire-server bundle set -format spiffe -id spiffe://upstream-domain.test -path /opt/spire/conf/server/upstream-domain.test.bundle
